package com.atguigu.mvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@Controller
public class TestController {

    @RequestMapping("/**/testInterceptor")
    public String testInterceptor() {
        return "success";
    }

    @RequestMapping("/hello")
    @ResponseBody
    public String hello() {
        return "hello";
    }


    @RequestMapping("/testExceptionHandler")
    public String testExceptionHandler(HttpServletRequest request) throws IOException {
        String type = request.getParameter("type");
        System.out.println("type = " + type);

        if ("1".equals(type)) {
            System.out.println(1 / 0);
        }
        if ("2".equals(type)) {
            throw new NullPointerException("出错啦: " + type);
        }

        if ("3".equals(type)) {
            throw new IndexOutOfBoundsException("IndexOutOfBoundsException: " + type);
        }
        if ("4".equals(type)) {
            throw new IOException("IOException: " + type);
        }

        return "success";
    }


}
